﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Mbs.Configuration;
using Mbs.Services;
using Mbs.Elements;

namespace Mbs
{
    public static partial class ServiceLocator
    {
        static ServiceLocator()
        {
            ClassLoader = new ClassLoader();
            MbsEnvironment.Init();
        }

        private static IServiceLocator locator;
        private static IServiceLocator Locator
        {
            get
            {
                if (locator == null)
                {
                    locator = ServiceLocatorSection.GetContainer();
                    if (locator == null)
                        throw new ArgumentNullException("service locator is null.");
                }
                return locator;
            }
            set
            {

                if (ServiceLocator.locator != null)
                    throw new Exception("Repeat initialize service locator exception.");
                if (value == null)
                    throw new ArgumentNullException("service locator is null.");
                locator = value; ;
            }
        }

       
        public static IClassLoader ClassLoader { get;private set;}

        public static IMessageBus MessageBus
        {
            get
            {
                var mb = AppDomain.CurrentDomain.GetData("mb") as IMessageBus;
                if (mb == null)
                {
                    mb = Locator.Get<IMessageBus>();
                    AppDomain.CurrentDomain.SetData("mb", mb);
                }
                return mb;
            }
        }

        public static IElementService ElementService
        {
            get
            {
                var mb = AppDomain.CurrentDomain.GetData("element.service") as IElementService;
                if (mb == null)
                {
                    mb = Locator.Get<IElementService>();
                    AppDomain.CurrentDomain.SetData("element.service", mb);
                }
                return mb;
            }
        }

        public static bool HasInitialized
        {
            get
            {
                return locator != null;
            }
        }

        public static void Initialize(IServiceLocator locator)
        {
            Locator = locator;
        }

        public static IServiceLocator Root
        {
            get { return Locator.Root; }
        }

        public static IServiceLocator Parent
        {
            get { return  Locator.Parent; }
        }

        public static void Release(object instance)
        {
            Locator.Release(instance);
        }

        public static void UnRegister(string id)
        {
            Locator.UnRegister(id);
        }

        public static void RegisterInstance(string id,object instance)
        {
            Locator.RegisterInstance(id, instance);
        }

        public static object Get(string id)
        {
            return Locator.Get(id);
        }

        public static object Get(Type serviceType)
        {
            return Locator.Get(serviceType);
        }

        public static object Get(Type serviceType, string name)
        {
            return Locator.Get(serviceType, name);
        }

        public static TService Get<TService>()
        {
            return Locator.Get<TService>();
        }

        public static TService Get<TService>(string name)
        {
            return Locator.Get<TService>(name);
        }

        public static void Clear()
        {
            Locator.Dispose();
        }
    }

}
